package com.feiyisys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyisys.common.PageResult;
import com.feiyisys.common.Result;
import com.feiyisys.entity.Category;
import com.feiyisys.entity.News;
import com.feiyisys.service.CategoryService;
import com.feiyisys.utils.TencentCosUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 文化遗产分类表 前端控制器
 * </p>
 *
 * @author wq
 * @since 2025-10-10
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Resource
    private TencentCosUtil tencentCosUtil;

    /**
     * 查询所有分类
     */
    @GetMapping
    public Result<List<Category>> getAll() {
        List<Category> categories = categoryService.list();
        return Result.success(categories);
    }


    /**
     * 根据 ID 查询分类
     */
    @GetMapping("/{id}")
    public Result<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 根据名字模糊查询分类
     */
    @GetMapping("/searchbyname")
    public Result<IPage<Category>> searchByName(@RequestParam String name,
                                             @RequestParam(defaultValue = "1") Long current,
                                             @RequestParam(defaultValue = "10") Long size) {
        Page<Category> page = new Page<>(current, size);
        IPage<Category> result = categoryService.page(page,
                new QueryWrapper<Category>().like("name", name));
        return Result.success(result);  // 修改为返回 Result<IPage<News>>，与getPage方法保持一致
    }
    /**
     * 新增分类
     */
    @PostMapping("/add")
    public Result<Boolean> createCategory(@RequestBody Category category) {
        boolean saved = categoryService.save(category);
        if(saved){
            return Result.success(saved);
        }else{
            return Result.fail("新增失败,请重试");
        }
    }

    /**
     * 更新分类
     */
    @PutMapping("update")
    public Result<Boolean> updateCategory(@RequestBody Category category) {
        boolean updated = categoryService.updateById(category);
        if(updated){
            return Result.success(updated);
        }else{
            return Result.fail("更新失败,请重试");
        }

    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteCategory(@PathVariable Long id) {
        boolean removed = categoryService.removeById(id);
        if(removed){
            return Result.success(removed);
        }else{
            return Result.fail("删除失败,请重试");
        }
    }
    @PostMapping("/upload")
    public Result<String> uploadPicture(@RequestParam("file") MultipartFile file) {
        try {
            // 上传文件到腾讯云COS
            String key = tencentCosUtil.uploadFile(file);
            // 获取文件完整访问URL
            String fileUrl = tencentCosUtil.getFileUrl(key);
            log.info("文件上传成功, URL: {}", fileUrl);
            return Result.success(fileUrl);
        } catch (Exception e) {
            log.error("文件上传失败: {}", e.getMessage());
            return Result.fail("文件上传失败: " + e.getMessage());
        }
    }
}
